package com.aiyige.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.media.MediaMetadataRetriever;

import com.aiyige.MyApp;
import com.aiyige.configs.MyConfig;
import com.vondear.rxtools.RxFileTool;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

/**
 * Created by zhuyakun on 2017/11/8.
 */

public class VideoUtil {
    public static final String COVER_DIR = MyConfig.AIYIGE_ROOT_DIR + ".videocover" + File.separator;
    public static final String NO_MEDIA_FILE = COVER_DIR + ".nomedia";

    public static String saveVideoCover(Bitmap bitmap) {
        RxFileTool.createOrExistsDir(COVER_DIR);
        RxFileTool.createOrExistsFile(NO_MEDIA_FILE);
        File outputFile = null;
        FileOutputStream fileOutputStream = null;
        String coverPath = "";
        try {
            outputFile = new File(COVER_DIR + UUID.randomUUID().toString().replace("-", "") + ".jpg");
            fileOutputStream = new FileOutputStream(outputFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
            coverPath = outputFile.getAbsolutePath();
        } catch (Exception e) {
        } finally {
            try {
                fileOutputStream.close();
            } catch (Exception ex) {

            }
        }
        return coverPath;
    }

    public static Bitmap getVideoFrame(String videoUrl, long position) {
        MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
        metadataRetriever.setDataSource(videoUrl);
        return metadataRetriever.getFrameAtTime(position * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
    }

    public static String getVideoCover(String videoUrl, long position) {
        Bitmap bitmap = getVideoFrame(videoUrl, position);
        if (bitmap == null) return "";
        String coverPath = saveVideoCover(bitmap);
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return coverPath;
    }


    public static Bitmap makeVideoCover(Bitmap inBmp, int outW, int outH) {
        float inW = inBmp.getWidth();
        float inH = inBmp.getHeight();
        Bitmap canvasBmp = Bitmap.createBitmap(outW, outH, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(canvasBmp);
        canvas.drawColor(0xff000000);
        Matrix matrix = new Matrix();
        if (inW > outW && inH > outH) {
            if (inW > inH) {
                float scaleFactor = outH / inH;
                matrix.postScale(scaleFactor, scaleFactor);
                matrix.postTranslate((outW - inW * scaleFactor) / 2f, (outH - inH * scaleFactor) / 2f);
            } else {
                float scaleFactor = outW / inW;
                matrix.postScale(scaleFactor, scaleFactor);
                matrix.postTranslate((outW - inW * scaleFactor) / 2f, (outH - inH * scaleFactor) / 2f);
            }
        } else if (inW > outW && inH <= outH) {
            float scaleFactor = outH / inH;
            matrix.postScale(scaleFactor, scaleFactor);
            matrix.postTranslate((outW - inW * scaleFactor) / 2f, (outH - inH * scaleFactor) / 2f);
        } else if (inW <= outW && inH > outH) {
            float scaleFactor = outW / inW;
            matrix.postScale(scaleFactor, scaleFactor);
            matrix.postTranslate((outW - inW * scaleFactor) / 2f, (outH - inH * scaleFactor) / 2f);
        } else {
            if (inW > inH) {
                float scaleFactor = outH / inH;
                matrix.postScale(scaleFactor, scaleFactor);
                matrix.postTranslate((outW - inW * scaleFactor) / 2f, (outH - inH * scaleFactor) / 2f);
            } else {
                float scaleFactor = outW / inW;
                matrix.postScale(scaleFactor, scaleFactor);
                matrix.postTranslate((outW - inW * scaleFactor) / 2f, (outH - inH * scaleFactor) / 2f);
            }
        }
        canvas.drawBitmap(inBmp, matrix, null);
        return canvasBmp;
    }

    public static void drawPlayOnVideoCover(Bitmap coverBmp, Bitmap playBmp) {
        Canvas canvas = new Canvas(coverBmp);
        Matrix matrix = new Matrix();
        matrix.postTranslate((coverBmp.getWidth() - playBmp.getWidth()) / 2f, (coverBmp.getHeight() - playBmp.getHeight()) / 2f);
        canvas.drawBitmap(playBmp, matrix, null);
    }

    public static String[] getVideoCover(String videoUrl, long position, int outW, int outH, int playResId) {
        Bitmap bitmap = getVideoFrame(videoUrl, position);
        if (bitmap == null) return null;
        Bitmap outBmp = makeVideoCover(bitmap, outW, outH);
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        String coverPath = saveVideoCover(outBmp);
        Bitmap playBmp = BitmapFactory.decodeResource(MyApp.getAppContext().getResources(), playResId);
        drawPlayOnVideoCover(outBmp, playBmp);
        String coverWithPlayBtnPath = saveVideoCover(outBmp);
        if (!outBmp.isRecycled()) {
            outBmp.recycle();
        }
        if (!playBmp.isRecycled()) {
            playBmp.recycle();
        }
        return new String[]{coverPath, coverWithPlayBtnPath};
    }


    public static void clearCoverDir() {
        RxFileTool.deleteFilesInDir(COVER_DIR);
    }
}
